<?php

//搜索页
class searchMod extends commonMod
{
    public function __construct()
    {
        parent::__construct();

    }

    //文章搜索
    public function index()
    {


        $keyword = in(urldecode($_GET['keyword'])); //获取搜索关键词
        $keyword = substrs($keyword ,15,'');
		$model = in($_GET['model']); //获取搜索类型
        
        //如果关键词为空，跳转到文章列表页面
        if (empty($keyword) || empty($model)) {
            $this->error('搜索关键词不能为空');
        }

        $listRows = 10; //每页显示的信息条数		
		$url = __URL__ . '/index/page-{page}.html?model=' . $model . '&keyword=' . rawurldecode($keyword); //分页基准网址
        $page = new Page();
        $cur_page = $page->getCurPage($url);
        $limit_start = ($cur_page - 1) * $listRows;
        $limit = $limit_start . ',' . $listRows;

		$where = "";
        $condition = array();

        if ($model == 1) //model=1 搜索标题+关键词

        {
            $where = 'WHERE A.title like "%' . $keyword . '%" or A.keywords like "%' . $keyword . '%" ';
            $condition = ' title like "%' . $keyword . '%"  or keywords like "%' . $keyword . '%" ';
            $count = $this->model->table('content')->where($condition)->count();
            //联合查询
            $sql = "SELECT  A.*,B.name as cname,B.seoname as cseoname,B.pinyin as cpinyin FROM {$this->model->pre}content A LEFT JOIN {$this->model->pre}channel B ON A.cid = B.id {$where} ORDER BY A.id DESC LIMIT {$limit}";

        }
		if ($model == 2) //model=2 搜索标题

        {
            $where = 'WHERE A.title like "%' . $keyword . '%" ';
            $condition = ' title like "%' . $keyword . '%" ';
            $count = $this->model->table('content')->where($condition)->count();
            //联合查询
            $sql = "SELECT  A.*,B.name as cname,B.seoname as cseoname,B.pinyin as cpinyin FROM {$this->model->pre}content A LEFT JOIN {$this->model->pre}channel B ON A.cid = B.id {$where} ORDER BY A.id DESC LIMIT {$limit}";

        }

        $loop = $this->model->query($sql);
        $this->assign('keyword', $keyword);
        $this->assign('loop', $loop);
        $this->assign('page', $this->page($url, $count, $listRows));
        
        if($this->config['LANG_OPEN']==1){
        $this->display(__LANG__.'/search');
        }else{
        $this->display('search');
        }

    }



}

?>